//
//  KLRemoteVideoCell.m
//  WebRTCDemo_iOS
//
//  Created by szblsx2 on 2020/3/9.
//  Copyright © 2020 szblsx2. All rights reserved.
//

#import "KLRemoteVideoCell.h"
#import <Masonry/Masonry.h>

@interface KLRemoteVideoCell ()<RTCVideoViewDelegate>
//渲染视图
@property (nonatomic, strong) RTCEAGLVideoView *videoView;

@property (nonatomic, strong) UIImageView *adminIcon;
//@property (nonatomic, strong) KLHeadView *headIcon;
@property (nonatomic, strong) UILabel *nameLabel;
//视频流
@property (nonatomic, strong) RTCVideoTrack *currentVideoTrack;

@end

@implementation KLRemoteVideoCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.videoView];
        self.clipsToBounds = YES;
        
        [self addSubview:self.adminIcon];
        [self.adminIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.equalTo(self);
            make.width.equalTo(@(40));
            make.height.equalTo(@(20));
        }];
//
//        [self addSubview:self.headIcon];
//        [self.headIcon mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.center.equalTo(self);
//            make.height.width.equalTo(@(60));
//        }];
//
        [self addSubview:self.nameLabel];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.adminIcon);
            make.left.equalTo(@(45));
        }];
        
    }
    return self;
}

- (void)reloadCellWithRemoteUser:(KLMeetUser *)remoteUser focus:(BOOL)focus
{
    if (_currentVideoTrack) {
        [_currentVideoTrack removeRenderer:_videoView];
    }
    if (remoteUser.videoTrack) {
        _videoView.hidden = NO;
        _currentVideoTrack = remoteUser.videoTrack;
    }else{
        _videoView.hidden = YES;
    }
    
    if (focus) {
        [_currentVideoTrack addRenderer:_videoView];
    }
    
    self.adminIcon.hidden = !remoteUser.isManager;
    self.nameLabel.text = remoteUser.userName;
    
    if (remoteUser.isManager) {
        [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(45));
        }];
    }else{
        [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(5));
        }];
    }
}

#pragma mark - RTCVideoViewDelegate

- (void)videoView:(id<RTCVideoRenderer>)videoView didChangeVideoSize:(CGSize)size
{
    NSLog(@"videoView size:%@", NSStringFromCGSize(size));
    RTCEAGLVideoView *view = (RTCEAGLVideoView *)videoView;
    CGRect bounds = view.bounds;
    [view setFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height)];
//    if (size.width > 0 && size.height > 0) {
//        // Aspect fill remote video into bounds.
//        CGRect bounds = view.bounds;
//        CGRect videoFrame = AVMakeRectWithAspectRatioInsideRect(size, view.bounds);
//        CGFloat scale = 1;
//
//        if (videoFrame.size.width > videoFrame.size.height) {
//            // Scale by height.
//            scale = bounds.size.height / videoFrame.size.height;
//        } else {
//            // Scale by width.
//            scale = bounds.size.width / videoFrame.size.width;
//        }
//
//        videoFrame.size.height *= scale;
//        videoFrame.size.width *= scale;
//        //        [view setBounds:(CGRect){0, 0, videoFrame.size.width, videoFrame.size.height}];
//        //        [view setCenter:(CGPoint){view.center.x + (videoFrame.size.width - bounds.size.width) * 0.5, view.center.y + (videoFrame.size.height - bounds.size.height) * 0.5}];
//        [view setFrame:CGRectMake(0, 0, videoFrame.size.width, videoFrame.size.height)];
//
//    }
}


#pragma mark - Getter

- (RTCEAGLVideoView *)videoView
{
    if (!_videoView) {
        _videoView = [[RTCEAGLVideoView alloc] initWithFrame:self.frame];
        _videoView.contentMode = UIViewContentModeScaleAspectFill;
        _videoView.delegate = self;
    }
    return _videoView;
}

- (UIImageView *)adminIcon
{
    if (!_adminIcon) {
        _adminIcon = [[UIImageView alloc] init];
        _adminIcon.image = [UIImage imageNamed:@"adminIcon"];
        _adminIcon.hidden = YES;
    }
    return _adminIcon;
}

//- (KLHeadView *)headIcon
//{
//    if (!_headIcon) {
//        _headIcon = [[KLHeadView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
//        _headIcon.hidden = YES;
//    }
//    return _headIcon;;
//}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.font = [UIFont systemFontOfSize:13];
    }
    return _nameLabel;
}

@end
